import { flowAdd } from '@/api/upload.ts'
import {message} from "antd";

const useConfigDataFile = (json: string) => {
    const JSONToFile = async (name: string, describe: string) => {
        const blob = new Blob([json], {type: "application/json"});
        const file = new File([blob], "example.json", { type: "application/json", lastModified: new Date().getTime() });
        const data = new FormData()
        data.append('file', file)
        data.append('name', name)
        data.append('describe', describe)
        const response = await flowAdd(data)
        // eslint-disable-next-line @typescript-eslint/no-unused-expressions
        response.code === 200 ? message.success('上传成功') : message.error('上传失败')
    }
    const dataFile = (data: string, fileName?: string): File => {
        const blob = new Blob([data], {type: "application/json"});
        const file = new File([blob],  fileName ? fileName : "example.json", { type: "application/json", lastModified: new Date().getTime() });
        return file
    }
    return { JSONToFile, dataFile }
}

export default useConfigDataFile